package com.awei.study.algorithm;

public class BinarySearch {

    /**
     *
     * @param a
     * @param target
     * @return 123
     */
    public static int binarySearchBasic(int a[], int target) {
        int i = 0, j = a.length - 1;
        while (i <= j) {
            int m = (i + j) >>> 1;
            if (target < a[m]) {
                j = m - 1;
            } else if (a[m] < target) {
                i = m + 1;
            } else {
                return m;
            }
        }
        return -1;
    }

    public static int binarySearchNative(int a[], int target) {
        int i = 0, j = a.length;
        while (i < j) {
            int m = (i + j) >>> 1;
            if (target < a[m]) {
                j = m;
            } else if (a[m] < target) {
                i = m + 1;
            } else {
                return m;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int a[] = new int[]{1,2,3,4,5};
        int r = binarySearchBasic(a,1);
        int r1 = binarySearchNative(a,1);
        System.out.println(r);
        System.out.println(r1);
    }
}
